package com.demo.reduction;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public class TestReduce {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
        int sum = numbers.stream()
                .reduce(0, (a, b) -> a + b);
        System.out.println("sum: " + sum);

        int product = numbers.stream()
                              .reduce(1, (a, b) -> a * b);
        System.out.println("multiply: " + product);

        sum = numbers.stream().reduce(0, Integer::sum);
        System.out.println(sum);
        numbers.stream().reduce((a, b) -> a * b)
                        .ifPresent(System.out::println);

        Optional<Integer> max = numbers.stream().reduce(Integer::max);
        max.ifPresent(n -> System.out.println("max: " + n));

        Optional<Integer> min = numbers.stream().reduce(Integer::min);
        min.ifPresent(n -> System.out.println("min: " + n));
    }
}
